package by.jabbby.ui.act;

import java.util.ArrayList;
import java.util.List;

import android.R.bool;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TextView;
import by.jabbby.Logger;
import by.jabbby.dataprovider.AccountProvider;
import by.jabbby.service.JabberService;
import by.jabbby.service.JabberServiceProxy;
import by.jabbby.service.Status;

public abstract class TitleBar extends Activity implements JabberServiceProxy.IJabberServiceBind {

	protected JabberServiceProxy jabberProxy;
	protected BR serviceReciver;
	MainMenuUtil mmu;
	public List<Integer> unVisibleMenuItems = new ArrayList<Integer>();

	protected class BR extends BroadcastReceiver {
		@Override
		public void onReceive(Context arg0, Intent intent) {
			if (JabberService.ACTION_CHANGE_STATUS.equals(intent.getAction())) {
				TitleBar.this.updateTitleBar();
			}

			if (intent.getAction().startsWith("jabby.actions.connection")) {
				TitleBar.this.updateTitleBar();
			}

		}

	}

	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
			
			jabberProxy = JabberServiceProxy.getInstance(this.getApplicationContext());
			mmu = new MainMenuUtil(this, jabberProxy);

		} catch (Exception e) {
			Logger.e(e);
		}

	}
	public void onJabberServiceBinded(){
		updateTitleBar();
	}

	protected void registerTitlebarRecivers() {
		IntentFilter intent = new IntentFilter();
		intent.addAction(JabberService.ACTION_CHANGE_STATUS);
		intent.addAction(JabberService.ACTION_CONNECTION_CLOSED);
		intent.addAction(JabberService.ACTION_CONNECTION_CLOSED_WITH_ERROR);
		intent.addAction(JabberService.ACTION_CONNECTION_LOGIN);
		intent.addAction(JabberService.ACTION_CONNECTION_LOGIN_FAILED);
		intent.addAction(JabberService.ACTION_CONNECTION_RECONNECT);
		intent.addAction(JabberService.ACTION_CONNECTION_RECONNECT_FAILED);
		intent.addAction(JabberService.ACTION_CONNECTION_RECONNECT_INSECOND);
		intent.addAction(JabberService.ACTION_CONNECTION_RECONNECT_SUCCESSFULL);
		// intent.addAction(JabberService.ACTION_);
		// intent.addAction(JabberService.ACTION_);

		if (serviceReciver == null) {
			serviceReciver = new BR();
		}
		this.registerReceiver(serviceReciver, intent);

	}

	protected void unregisterTitleBarRecivers() {
		try {
			if (serviceReciver != null) {
				// serviceReciver.
				this.unregisterReceiver(serviceReciver);
			}
		} catch (Exception e) {
			Logger.e(e);
		}
	}

	public TextView getLeftTextView() {
		TextView tv = (TextView) findViewById(R.id.title_bar_left_text);
		return tv;
	}

	public TextView getRightTextView() {
		TextView tv = (TextView) findViewById(R.id.title_bar_right_text);
		return tv;
	}

	public void initTitleBar() {
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);

	}

	private void updateTitleBar() {
		try {
			AccountProvider ap = new AccountProvider(this.getApplicationContext());
			String accountStr = ap.getCurrentAccount().getConnectionString();
		//	getLeftTextView().setText(accountStr);
			if (jabberProxy.isServiceBinded()) {
				String statusStr = jabberProxy.getService().getCurrentStatus();

				Status status = Status.valueOf(statusStr);
				String appName = TitleBar.this.getString(R.string.app_name);
				TitleBar.this.getRightTextView().setText(status.getLocalization(TitleBar.this));
				switch (status) {
				case OnLine:
					TitleBar.this.getRightTextView().setTextColor(Color.GREEN);
					break;
				case OfLine:
					TitleBar.this.getRightTextView().setTextColor(Color.RED);
					break;
				}
			}
		} catch (RemoteException e) {
			Logger.e(e);
		} catch (Exception e) {
			Logger.e(e);
		}
	}

	@Override
	protected void onPause() {
		try{
		super.onPause();
		unregisterTitleBarRecivers();
		JabberServiceProxy.removeBinderListener(this);
		}catch(Exception e){
			Logger.e(e);
		}

	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
	
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onStart() {
		Logger.e("TitleBar.onStart()");
		super.onStart();
		JabberServiceProxy.addBinderListener(this);
		updateTitleBar();
		registerTitlebarRecivers();
		

	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		mmu.onMenuItemSelected(featureId, item);
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuInflater inflater = getMenuInflater();

		inflater.inflate(R.menu.primary_menu, menu);

		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);

		try {
			for (Integer itemId : unVisibleMenuItems) {
				MenuItem mi = menu.findItem(itemId);
				mi.setVisible(false);
			}
		} catch (Exception e) {
			Logger.e(e);
		}
		return true;
	}

	protected void setVisibleMenuItemsOnly(int... vivsible) {
		unVisibleMenuItems.clear();
		for (Integer itemId : MainMenuUtil.mainMenuItems) {
			boolean f = false;
			for (Integer visibleItemId : vivsible) {
				if (visibleItemId.equals(itemId)) {
					f = true;
					continue;
				}
			}
			if (!f) {
				unVisibleMenuItems.add(itemId);
			}
		}
		return;

	}
	/*
	 * public void setLeftTitle(String text){ TextView tv = (TextView)
	 * findViewById(R.id.title_bar_left_text); tv.setText(text); }
	 * 
	 * public void setRightTitle(String text){ TextView tv = (TextView)
	 * findViewById(R.id.title_bar_right_text); tv.setText(text); }
	 */
}
